//
//  TestViewController.m
//  Drivinglicense
//
//  Created by HappyMilk 02 on 2017/6/28.
//  Copyright © 2017年 tomi. All rights reserved.
//

#import "TestViewController.h"
#import "QuestionModel.h"
#import "LoginHeader.h"
#import <UIImageView+WebCache.h>
#import "MJRequestServer.h"
#import <SVProgressHUD.h>

@interface TestViewController ()

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIView *view_bg;
@property (weak, nonatomic) IBOutlet UILabel *lab_question;
@property (weak, nonatomic) IBOutlet UIImageView *imgv_ques;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imgv_height;
@property (weak, nonatomic) IBOutlet UILabel *lab_ans1;
@property (weak, nonatomic) IBOutlet UILabel *lab_ans2;
@property (weak, nonatomic) IBOutlet UILabel *lab_ans3;
@property (weak, nonatomic) IBOutlet UILabel *lab_ans4;
@property (weak, nonatomic) IBOutlet UIButton *btn_ans1;
@property (weak, nonatomic) IBOutlet UIButton *btn_ans2;
@property (weak, nonatomic) IBOutlet UIButton *btn_ans3;
@property (weak, nonatomic) IBOutlet UIButton *btn_ans4;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *btn_ans_arr;

//答案界面
@property (weak, nonatomic) IBOutlet UIView *view_answer;
@property (weak, nonatomic) IBOutlet UILabel *lab_corect_ques;
@property (weak, nonatomic) IBOutlet UILabel *lab_ques_detail;

//考题序号
@property (nonatomic) NSInteger num_question;
//当前题目
@property (nonatomic ,strong) QuestionModel *currentModel;
//提交
@property (weak, nonatomic) IBOutlet UIButton *btn_submit;

//翻页
@property (nonatomic) NSInteger loadPage;

@property (nonatomic ,strong) GADBannerView *bannerView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scroll_top_const;


@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];self.loadPage = 2;
    // Do any additional setup after loading the view.
    if (IS_PASS) {
        self.scroll_top_const.constant = IS_IPAD?100:60;
    }else{
        self.scroll_top_const.constant = 16;
    }
    
    
    [self modifyView];
    
    [self loadQuestion:0];
    
    if (IS_PASS && IS_CHINESE) {
        self.bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait origin:(CGPoint){0,66}];
        [self.view addSubview:_bannerView];
        _bannerView.adUnitID = AD_TEST_HF;
        _bannerView.rootViewController = self;
        [_bannerView loadRequest:[GADRequest request]];
    }

}
- (void)modifyView{
    
    self.view_bg.layer.cornerRadius = 6;
    self.view_bg.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    self.view_bg.layer.shadowOffset = CGSizeMake(0,4);
    self.view_bg.layer.shadowRadius = 4;
    self.view_bg.layer.shadowOpacity = 0.8;
}

- (void)loadQuestion:(NSInteger)num{
    self.num_question = num;
    
    //刷新勾选按钮
    for (UIButton *btn in self.btn_ans_arr) {
        btn.selected = NO;
    }
    
    //隐藏答案界面
    self.view_answer.hidden = YES;
    self.view_answer.alpha = 0;
    
    QuestionModel *model = self.testArray[num];
    self.currentModel = model;
    //题目
    self.lab_question.text = [NSString stringWithFormat:@"%ld、%@",num+1,model.question];
    //图片
    if (IsNilString(model.pic)) {
        self.imgv_height.constant = 0;
    }else{
        self.imgv_height.constant = 190;
        [self.imgv_ques sd_setImageWithURL:[NSURL URLWithString:model.pic]];
    }
    //答案选项
    if ([model.answer isEqualToString:@"对"] | [model.answer isEqualToString:@"错"]) {
        self.lab_ans1.text = @"对";
        self.lab_ans2.text = @"错";
        self.lab_ans3.hidden = YES;
        self.lab_ans4.hidden = YES;
        self.btn_ans3.hidden = YES;
        self.btn_ans4.hidden = YES;
    }else{
        self.lab_ans3.hidden = NO;
        self.lab_ans4.hidden = NO;
        self.btn_ans3.hidden = NO;
        self.btn_ans4.hidden = NO;
        self.lab_ans1.text = model.option1;
        self.lab_ans2.text = model.option2;
        self.lab_ans3.text = model.option3;
        self.lab_ans4.text = model.option4;
    }
    //正确答案
    self.lab_corect_ques.text = [NSString stringWithFormat:@"答案：%@",model.answer];
    self.lab_ques_detail.text=  [NSString stringWithFormat:@"详解：%@",model.explain];
    
    //判断剩余试题数量请求更多题目
    if (self.testArray.count-self.num_question < 20 && self.testType == 0) {
        [self getMoreQuestionsData:self.testType];
    }
}

- (void)getMoreQuestionsData:(NSInteger)testTpye{
    
    NSDictionary *dic = @{@"type":@"C1",
                          @"subject":@"1",
                          @"pagesize":@"30",
                          @"pagenum":@"1",
                          @"sort":@"rand",
                          @"appkey":@"63a288a68764b5e2"};
    
    [MJRequestServer Post:dic success:^(id sender) {
        
        self.loadPage++;
        [self.testArray addObjectsFromArray:[QuestionModel mj_objectArrayWithKeyValuesArray:sender]];
        
    } andFailure:^(id sender) {
        
    }];
}



- (IBAction)popvc:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

//确认提交答案
- (IBAction)submitAnswer:(UIButton *)sender {
    BOOL is_selected = NO;
    for (UIButton *btn in self.btn_ans_arr) {
        if (btn.selected) {
            is_selected = YES;
            break;
        }
    }
    if (is_selected) {
        [self chickAnswer:nil];
    }else{
        [SVProgressHUD showInfoWithStatus:@"请选择答案"];
    }
}
//上一题
- (IBAction)beforeTest:(UIButton *)sender {
    
    if (IS_PASS) {
        if ([[MyAdTools shareInstance] showInterstitialWithVC:self]) {
            return;
        }
    }
    
    if (self.num_question-1>=0) {
        [self loadQuestion:self.num_question-1];
    }
}
//下一题
- (IBAction)nextTest:(UIButton *)sender {
    if (IS_PASS) {
        if ([[MyAdTools shareInstance] showInterstitialWithVC:self]) {
            return;
        }
    }
    
    if (self.num_question+1<self.testArray.count) {
        [self loadQuestion:self.num_question+1];
    }
}

//查看答案
- (IBAction)chickAnswer:(UIButton *)sender {
    [UIView animateWithDuration:0.3 animations:^{
        self.view_answer.hidden = NO;
        self.view_answer.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}

- (IBAction)answerQuestion:(UIButton *)sender {
    for (UIButton *btn in self.btn_ans_arr) {
        if ([btn isEqual:sender]) {
            btn.selected = YES;
        }else{
            btn.selected = NO;
        }
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
